/**
 * @Author: douxuefeng
 * @Date: 2020/9/16 下午3:27
 * @File: server
 * @Desc:
 */
package server

import (
	go_common "gitee.com/douxuefeng/go-common"
	"gitee.com/douxuefeng/go-common/wechat/wechat_office/config"
	"sort"
	"strings"
)

type OfficeAccountServer struct {
	cfg *config.WechatOfficeConfig
}

func NewOfficeAccountServer(cfg *config.WechatOfficeConfig) *OfficeAccountServer {
	return &OfficeAccountServer{cfg}
}

func (oa *OfficeAccountServer) Serve(data *CheckSignature) string {
	var s = []string{oa.cfg.Token, data.Timestamp, data.Nonce}
	sort.Strings(s)
	ss := strings.Join(s, "")
	if go_common.Sha1(ss) == data.Signature {
		return data.Echostr
	}
	return ""
}

type CheckSignature struct {
	Signature string `url:"signature"`
	Timestamp string `url:"timestamp"`
	Nonce string `url:"nonce"`
	Echostr string `url:"echostr"`
 }